package com.hd.pzhwlxt.controller.model2;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.MaterialOrder;
import com.hd.pzhwlxt.pojo.MaterialPurchase;
import com.hd.pzhwlxt.service.MaterialOrderService;
import com.hd.pzhwlxt.service.MaterialPurchaseService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @create: 2022-09-26 9:04
 * @author: 怀念天空
 * @Description:
 */
@RestController
public class MaterialController {

    @Autowired
    private MaterialOrderService materialOrderService;

    @Autowired
    private MaterialPurchaseService materialPurchaseService;

    /**
     * 返回材料详情和材料收货方信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/materialOrder")
    public Response<Object> getAllOrder(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<MaterialOrder> result = materialOrderService.getAllOrder();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<MaterialOrder> result = materialOrderService.getAllOrderPage(pageNum, pageSize);
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/materialOrder")
    public Response<String> addMaterialOrder(@RequestBody MaterialOrder materialOrder){
        boolean save = materialOrderService.addMaterialOrder(materialOrder);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    /**
     * 采购部分
     */
    @GetMapping("/materialPurchase")
    public Response<Object> getAllPurchaseInfo(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<MaterialPurchase> result = materialPurchaseService.getAllPurchaseInfo();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<MaterialPurchase> result = materialPurchaseService.getAllPurchaseInfoPage(new Page<>(pageNum, pageSize));
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/materialPurchase")
    public Response<String> addPurchaseInfo(@RequestBody MaterialPurchase materialPurchase){
        //添加时先计算金额
        boolean save = materialPurchaseService.addPurchaseInfo(materialPurchase);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @PutMapping("/materialPurchase")
    public Response<String> updatePurchaseInfo(@RequestBody MaterialPurchase materialPurchase){
        //修改时添加库存信息
        boolean update = materialPurchaseService.updatePurchaseInfo(materialPurchase);
        if (update){
            return Response.createSuccessResponse("更新成功");
        }
        return Response.createErrorResponse("更新失败");
    }
}
